package com.frostmourne.loreoriginex.objects.managers;

import com.frostmourne.loreoriginex.LoreOriginEX;
import com.frostmourne.loreoriginex.objects.LoreOrigin;
import com.frostmourne.loreoriginex.objects.LoreOriginData;
import com.frostmourne.loreoriginex.utils.MathUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoreOriginManager {

    private static Map<String , LoreOrigin> loreOriginMap = new HashMap<>();

    public void load(){
        loreOriginMap.clear();

        FileConfiguration config = LoreOriginEX.getInstance().getConfig();
        if (config.getKeys(false).contains("Attributes")){

            for (String key : config.getConfigurationSection("Attributes").getKeys(false)){
                LoreOrigin origin = new LoreOrigin(key,config.getStringList("Attributes." + key));
                loreOriginMap.put(key,origin);
                System.out.println("[LoreOriginEX]成功载入属性:" + origin.getName());
                System.out.println("[LoreOriginEX]       关键字: " + origin.getKeys());
            }

        }

    }

    public List<LoreOriginData> getAttributeByLore(List<String> lore){

        List<LoreOriginData> data = new ArrayList<>();
        for (String s : lore){
            LoreOriginData loreOriginData = getDataByLine(s);
            if (loreOriginData == null){
                continue;
            }
            data.add(loreOriginData);
        }
        return data;

    }

    public LoreOriginData getDataByLine(String line){

        line = ChatColor.stripColor(line);
        LoreOriginData data = null;

        for (String key : loreOriginMap.keySet()){

            for (String alias : loreOriginMap.get(key).getKeys()){

                if (line.contains(alias)){

                    line = line.replace(alias,"");
                    List<Double> doubles = MathUtils.matchNumber(line);
                    data = new LoreOriginData(loreOriginMap.get(key),doubles);

                }

            }

        }
        return data;
    }

    public static Map<String, LoreOrigin> getLoreOriginMap() {
        return loreOriginMap;
    }
}
